import Big from 'big.js'

class Factory {
  constructor (number) {
    this.number = new Big(number ?? 0)
  }

  add(num) {
    this.number = this.number.plus(num)
    return this
  }

  sub(num) {
    this.number = this.number.minus(num)
    return this
  }

  mul(num) {
    this.number = this.number.times(num)
    return this
  }

  div(num) {
    this.number = this.number.div(num)
    return this
  }

  pro(num) {
    this.mul(new Big(num).div(100))
    return this
  }

  addpro(num) {
    this.mul(new Big(100).plus(num).div(100))
    return this
  }

  subpro(num) {
    this.mul(new Big(100).minus(num).div(100))
    return this
  }

  divpro(num) {
    this.div(new Big(100).plus(num).div(100))
    return this
  }

  format(num = 2) {
    return Number(this.number.toFixed(num))
  }

  formatSmall() {
    return Number(this.number.toFixed(2, 0))
  }

  value() {
    return this.number
  }
}

const add = (a, b) => {
  return factory(a).add(b).format()
}

const sub = (a, b) => {
  return factory(a).sub(b).format()
}

const mul = (a, b) => {
  return factory(a).mul(b).format()
}

const div = (a, b) => {
  return factory(a).div(b).format()
}

const pro = (a, b) => {
  return factory(a).pro(b).format()
}

const subpro = (a, b) => {
  return factory(a).subpro(b).format()
}

const divpro = (a, b) => {
  return factory(a).divpro(b).format()
}

const addpro = (a, b) => {
  return factory(a).addpro(b).format()
}

const format = (a) => {
  return factory(a).format()
}

const formatSmall = (a) => {
  return factory(a).formatSmall()
}

const factory = (a) => {
  return new Factory(a)
}

export default {
  add,
  sub,
  mul,
  div,
  pro,
  subpro,
  divpro,
  addpro,
  format,
  formatSmall,
  factory,
};